//
//  HorizontalScrollerCollectionView.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/25.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "HorizontalScrollerCollectionView.h"
#import "BLHeader.h"
#import <Masonry.h>

@interface HorizontalScrollerCollectionView () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *titleView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation HorizontalScrollerCollectionView 

+ (instancetype) horizantalScrollerCollection {
    HorizontalScrollerCollectionView *horizontalView = [[[NSBundle mainBundle] loadNibNamed:@"HorizontalScrollerCollectionView" owner:nil options:nil] lastObject];
    return horizontalView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupUI];
}


- (void)setupUI {
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.itemSize = CGSizeMake(KScreenWidth, KScreenHeight - 49 - 64);
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ReuseIdentify];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.currentPage = 0;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ReuseIdentify forIndexPath:indexPath];
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UIViewController *controller = self.controllers[indexPath.item];
    [cell.contentView addSubview:controller.view];
    [controller.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(cell.contentView);
    }];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.controllers count];
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    NSInteger index = targetContentOffset->x / KScreenWidth;
    UIButton *button = self.titleView.subviews[index];
    [self titleButtonClick:button];
}


- (void)setControllers:(NSArray *)controllers {
    _controllers = controllers;
    [self.collectionView reloadData];
}

- (void)setTitles:(NSArray *)titles {
    _titles = titles;
    NSInteger count = _titles.count;
    _width = KScreenWidth / count;
    for (int i = 0; i < count; i++) {
        UIButton *tView = [[UIButton alloc] initWithFrame:CGRectMake(i * _width, 0, _width, 47)];
        [tView setTitle:_titles[i] forState:UIControlStateNormal];
        tView.titleLabel.font = [UIFont systemFontOfSize:15];
        [tView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [tView setTitleColor:kAppThemeColor forState:UIControlStateSelected];
        tView.tag = i;
        [tView addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleView addSubview:tView];
    }
    [(UIButton *)self.titleView.subviews[self.currentPage] setSelected:YES];
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 47, _width, 2)];
    UIView *bgLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 47, KScreenWidth, 2)];
    bgLineView.backgroundColor = [UIColor colorWithRed:253.0/255 green:235.0/255 blue:185.0/255 alpha:1.0];
    [self.titleView addSubview:bgLineView];
    self.bgView.backgroundColor = kAppThemeColor;
    [self.titleView addSubview:_bgView];
    [self layoutIfNeeded];
    [self.collectionView reloadData];
}

- (void)titleButtonClick:(UIButton *)button {
    if (button.selected) {
        return;
    }
    [button setSelected:YES];
    UIButton *preBtn = self.titleView.subviews[_currentPage];
    [preBtn setSelected:NO];
    NSInteger tag = button.tag;
    [UIView animateWithDuration:0.2 animations:^{
        self.bgView.frame = CGRectMake(tag * _width, 47, _width, 2);
    }];
    self.currentPage = tag;
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:tag inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
    [self.collectionView reloadData];

}

- (void)collectionFrame {
   // NSLog(@"collection = %@", NSStringFromCGRect(self.collectionView.frame));
}

@end
